using HKB.Common;
using HKB.Data.Dal;
using HKB.Data.Model;
using System;
using System.Collections.Generic;
using System.Data;
namespace HKB.Data.Bll
{
	public class StockItemOrder
	{
		private readonly HKB.Data.Dal.StockItemOrder dal = new HKB.Data.Dal.StockItemOrder();
		public int GetMaxId()
		{
			return this.dal.GetMaxId();
		}
		public bool Exists(int ItemOrderID)
		{
			return this.dal.Exists(ItemOrderID);
		}
		public int Add(HKB.Data.Model.StockItemOrder model)
		{
			return this.dal.Add(model);
		}
		public int AddInfoTransaction(HKB.Data.Model.StockItemOrder model, Conn conn)
		{
			return this.dal.AddInfoTransaction(model, conn);
		}
		public bool Update(HKB.Data.Model.StockItemOrder model)
		{
			return this.dal.Update(model);
		}
		public bool Delete(int ItemOrderID)
		{
			return this.dal.Delete(ItemOrderID);
		}
		public bool DeleteList(string ItemOrderIDlist)
		{
			return this.dal.DeleteList(ItemOrderIDlist);
		}
		public HKB.Data.Model.StockItemOrder GetModel(int ItemOrderID)
		{
			return this.dal.GetModel(ItemOrderID);
		}
		public HKB.Data.Model.StockItemOrder GetModelByCache(int ItemOrderID)
		{
			string cacheKey = "StockItemOrderModel-" + ItemOrderID;
			object obj = DataCache.GetCache(cacheKey);
			if (obj == null)
			{
				try
				{
					obj = this.dal.GetModel(ItemOrderID);
					if (obj != null)
					{
						int configInt = ConfigHelper.GetConfigInt("ModelCache");
						DataCache.SetCache(cacheKey, obj, System.DateTime.Now.AddMinutes((double)configInt), System.TimeSpan.Zero);
					}
				}
				catch
				{
				}
			}
			return (HKB.Data.Model.StockItemOrder)obj;
		}
		public DataSet GetList(string strWhere)
		{
			return this.dal.GetList(strWhere);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			return this.dal.GetList(Top, strWhere, filedOrder);
		}
		public System.Collections.Generic.List<HKB.Data.Model.StockItemOrder> GetModelList(string strWhere)
		{
			DataSet list = this.dal.GetList(strWhere);
			return this.DataTableToList(list.Tables[0]);
		}
		public System.Collections.Generic.List<HKB.Data.Model.StockItemOrder> DataTableToList(DataTable dt)
		{
			System.Collections.Generic.List<HKB.Data.Model.StockItemOrder> list = new System.Collections.Generic.List<HKB.Data.Model.StockItemOrder>();
			int count = dt.Rows.Count;
			if (count > 0)
			{
				for (int i = 0; i < count; i++)
				{
					HKB.Data.Model.StockItemOrder stockItemOrder = this.dal.DataRowToModel(dt.Rows[i]);
					if (stockItemOrder != null)
					{
						list.Add(stockItemOrder);
					}
				}
			}
			return list;
		}
		public DataSet GetAllList()
		{
			return this.GetList("");
		}
		public int GetRecordCount(string strWhere)
		{
			return this.dal.GetRecordCount(strWhere);
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
		}
		public int AddStockOrderItem(int infoOrderID, string infoOrderNumber, string ProductStr)
		{
			int result = 0;
			if (!string.IsNullOrEmpty(ProductStr))
			{
				string[] array = ProductStr.Split(new char[]
				{
					'|'
				});
				for (int i = 0; i < array.Length; i++)
				{
					string[] array2 = array[i].Split(new char[]
					{
						','
					});
					HKB.Data.Model.StockItemOrder stockItemOrder = new HKB.Data.Model.StockItemOrder();
					if (array2.Length >= 4)
					{
						int value = System.Convert.ToInt32(array2[0]);
						int value2 = System.Convert.ToInt32(array2[1]);
						decimal value3 = System.Convert.ToDecimal(array2[2]);
						string unit = array2[3];
						stockItemOrder.InfoOrderID = new int?(infoOrderID);
						stockItemOrder.InfoNumber = infoOrderNumber;
						stockItemOrder.ProductID = new int?(value);
						stockItemOrder.StockCount = new int?(value2);
						stockItemOrder.Price = new decimal?(value3);
						stockItemOrder.Unit = unit;
						result = this.dal.Add(stockItemOrder);
					}
				}
			}
			return result;
		}
		public int StockItemAdd(int InfoOrderID, string InfoNumber, int s_productid, int s_stockCount, decimal s_price, string s_unit)
		{
			int result = 0;
			if (InfoOrderID > 0)
			{
				HKB.Data.Model.StockItemOrder stockItemOrder = new HKB.Data.Model.StockItemOrder();
				stockItemOrder.InfoOrderID = new int?(InfoOrderID);
				stockItemOrder.InfoNumber = InfoNumber;
				stockItemOrder.ProductID = new int?(s_productid);
				stockItemOrder.StockCount = new int?(s_stockCount);
				stockItemOrder.Price = new decimal?(s_price);
				stockItemOrder.Unit = s_unit;
				result = this.dal.Add(stockItemOrder);
			}
			return result;
		}
		public bool EditStockOrderItem(string itemIDStr, string ProductStr)
		{
			bool result = false;
			if (!string.IsNullOrEmpty(itemIDStr))
			{
				string[] array = itemIDStr.Split(new char[]
				{
					','
				});
				for (int i = 0; i < array.Length; i++)
				{
					int itemOrderID = System.Convert.ToInt32(array[i]);
					HKB.Data.Model.StockItemOrder model = this.dal.GetModel(itemOrderID);
					if (model != null)
					{
						if (!string.IsNullOrEmpty(ProductStr))
						{
							string[] array2 = ProductStr.Split(new char[]
							{
								'|'
							});
							for (int j = 0; j < array2.Length; j++)
							{
								string[] array3 = array2[j].Split(new char[]
								{
									','
								});
								if (array3.Length >= 4)
								{
									int num = System.Convert.ToInt32(array3[0]);
									int value = System.Convert.ToInt32(array3[1]);
									decimal value2 = System.Convert.ToDecimal(array3[2]);
									string unit = array3[3];
									if (model.ProductID == num)
									{
										model.ItemOrderID = itemOrderID;
										model.ProductID = new int?(num);
										model.StockCount = new int?(value);
										model.Price = new decimal?(value2);
										model.Unit = unit;
										result = this.dal.Update(model);
										break;
									}
								}
							}
						}
					}
				}
			}
			return result;
		}
	}
}
